<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Agp\CoreBundle\Entity;

use Symfony\Component\Security\Core\Role\RoleInterface;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Agp\CoreBundle\Entity\Grupo
 *
 * @ORM\Table(name="grupo")
 * @ORM\Entity(repositoryClass="Agp\CoreBundle\Repository\GrupoRepository")
 */
class Grupo extends \Symfony\Component\Security\Core\Role\Role implements RoleInterface, \Serializable{
    
    /**
     * @ORM\Column(name="cod_grupo", type="integer")
     * @ORM\Id()
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $codGrupo;

    /**
     * @ORM\Column(name="nome", type="string", length=45)
     */
    protected $nome;

    /**
     * @ORM\Column(name="role", type="string", length=45, unique=true)
     */
    protected $role;

    /**
     * @ORM\ManyToMany(targetEntity="Pessoa", mappedBy="grupos")
     * @ORM\JoinTable(name="pessoa_grupo",
     *     joinColumns={@ORM\JoinColumn(name="cod_pessoa", referencedColumnName="cod_pessoa")},
     *     inverseJoinColumns={@ORM\JoinColumn(name="cod_grupo", referencedColumnName="cod_grupo")}
     * )
     * @var ArrayCollection $pessoas
     */
    protected $pessoas;
    
    const ROLE_SOCIO_MASTER =        'ROLE_SOCIO_MASTER';
    const ROLE_SOCIO =               'ROLE_SOCIO';
    const ROLE_GERENTE =             'ROLE_GERENTE';
    const ROLE_SUPERVISOR =          'ROLE_SUPERVISOR';
    const ROLE_COLABORADOR =         'ROLE_COLABORADOR';
    const ROLE_SUPORTE =             'ROLE_SUPORTE';
    const ROLE_ADMINISTRADOR =       'ROLE_ADMIN';
    const ROLE_SUPER_ADMINISTRADOR = 'ROLE_SUPER_ADMIN';
    

    public function __construct()
    {
        $this->pessoas = new ArrayCollection();
    }
    
    static function getPrioridade($role){
        
        switch ($role){
            case self::ROLE_COLABORADOR:
                return 1;
                break;
            case self::ROLE_SUPERVISOR:
                return 2;
                break;
            case self::ROLE_GERENTE:
                return 3;
                break;
            case self::ROLE_SOCIO:
                return 4;
                break;
            case self::ROLE_SOCIO_MASTER:
                return 5;
                break;
            case self::ROLE_SUPORTE:
                return 6;
                break;
            case self::ROLE_ADMINISTRADOR:
                return 7;
                break;
            case self::ROLE_SUPER_ADMINISTRADOR:
                return 8;
                break;
            
        }
        
    }

    public function __toString() {
        return $this->nome;
    }

        /**
     * Set codGrupo
     *
     * @param integer $codGrupo
     * @return Grupo
     */
    public function setCodGrupo($codGrupo)
    {
        $this->codGrupo = $codGrupo;
        return $this;
    }
    
    
    /**
     * Get codGrupo
     *
     * @return integer 
     */
    public function getCodGrupo()
    {
        return $this->codGrupo;
    }
    
    /**
     * Set nome
     *
     * @param integer $nome
     * @return string
     */
    public function setNome($nome)
    {
        $this->nome = $nome;
        return $this;
    }

    /**
     * Get nome
     *
     * @return string 
     */
    public function getNome()
    {
        return $this->nome;
    }
    
    /**
     * Get pessoas
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getPessoas()
    {
        return $this->pessoas;
    }

    /**
     * Add pessoa
     *
     * @param Agp\CoreBundle\Entity\Pessoa $pessoas
     */
    public function addPessoa(\Agp\CoreBundle\Entity\Pessoa $pessoa)
    {
        $this->pessoas[] = $pessoa;
    }

    /**
     * @see RoleInterface
     */
    public function getRole()
    {
        return $this->role;
    }
    
    static function geraAclDefault($role){
        
        $acls = self::getFullAcls();
        //var_dump($acls);
        switch ($role){
            case self::ROLE_COLABORADOR:
                $acls['Agp\CoreBundle\Entity\Atendente'] = array();
                $acls['Agp\CoreBundle\Entity\Evento'] = array();
                $acls['Agp\CoreBundle\Entity\ListaPreco'] = array();
                $acls['Agp\CoreBundle\Entity\Produto'] = array();
                $acls['Agp\CoreBundle\Entity\Pessoa'] = array('RELATORIO');
                $acls['Agp\CoreBundle\Entity\Terminal'] = array('VIEW');
                $acls['Agp\CoreBundle\Entity\ProdutoListaPreco'] = array();
                $acls['Agp\CoreBundle\Entity\HabilitaProdutoTerminal'] = array('VIEW');
                break;
            
            case self::ROLE_SUPERVISOR:
                $acls['Agp\CoreBundle\Entity\Atendente'] = array();
                $acls['Agp\CoreBundle\Entity\Evento'] = array();
                $acls['Agp\CoreBundle\Entity\ListaPreco'] = array();
                $acls['Agp\CoreBundle\Entity\Produto'] = array();
                $acls['Agp\CoreBundle\Entity\Pessoa'] = array('RELATORIO');
                $acls['Agp\CoreBundle\Entity\Terminal'] = array('VIEW');
                $acls['Agp\CoreBundle\Entity\ProdutoListaPreco'] = array();
                $acls['Agp\CoreBundle\Entity\HabilitaProdutoTerminal'] = array('VIEW','EDIT');
                break;
            
            case self::ROLE_GERENTE:
                break;
            
            case self::ROLE_SOCIO:
                break;
            
            case self::ROLE_SOCIO_MASTER:
                break;
            
            case self::ROLE_SUPORTE:
                break;
            
            case self::ROLE_ADMINISTRADOR:
                break;
            
            case self::ROLE_SUPER_ADMINISTRADOR:
                break;

        }
        return $acls;
    }
    
    static function getFullAcls()
    {
        $acls['Agp\CoreBundle\Entity\Atendente'] = array('CREATE','EDIT','VIEW','DELETE','LIST');
        $acls['Agp\CoreBundle\Entity\Evento'] = array('CREATE','EDIT','VIEW','DELETE','LIST');
        $acls['Agp\CoreBundle\Entity\ListaPreco'] = array('CREATE','EDIT','VIEW','DELETE','LIST');
        $acls['Agp\CoreBundle\Entity\Produto'] = array('CREATE','EDIT','VIEW','DELETE','LIST');
        $acls['Agp\CoreBundle\Entity\Pessoa'] = array('CREATE','EDIT','VIEW','DELETE','LIST','SENHA','PERMISSAO','RELATORIO','TERMINAL');
        $acls['Agp\CoreBundle\Entity\Terminal'] = array('VIEW');
        $acls['Agp\CoreBundle\Entity\ProdutoListaPreco'] = array('CREATE','EDIT','DELETE');
        $acls['Agp\CoreBundle\Entity\HabilitaProdutoTerminal'] = array('VIEW','EDIT');
        
        return $acls;
    }
    
   /**
     * @see \Serializable::serialize()
     */
    public function serialize()
    {
        /*
         * ! Don't serialize $users field !
         */
        return \serialize(array(
            $this->codGrupo,
            $this->nome,
            $this->role
        ));
    }

    /**
     * @see \Serializable::unserialize()
     */
    public function unserialize($serialized)
    {
        list(
            $this->codGrupo,
            $this->nome,
            $this->role
        ) = \unserialize($serialized);
    }
    
}

?>
